<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class CaseTypeController extends Controller{



    /*
     *  添加案件类型
     *
     *  @param Request $request
     *  @return json
     *
     * */
    public function add_case_type(Request $request){


        $data=$request->only('title','description');

        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,10}$/u',$data['title'])) return ThrowApiExc('20001');
        if(!preg_match('/^[\w \x{4e00}-\x{9fa5}]{2,255}$/u',$data['description'])) return ThrowApiExc('20002');


        $res = DB::table('C_type')->insertGetId(
            [
                'title'=>$data['title'],
                'description'=>$data['description']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }


    }

    /*
     * 查询全部的案件类型 返回json
     *
     * @return json
     *
     * */


    public function  select_case_type(){

        $data=DB::table('C_type')->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }

    /*
     * 修改案件类型的状态以及内容
     *
     * @param Request $request
     * @return json
     *
     * */

    public function update_case_type(Request $request,$tid){


        $data=$request->only('title','description');
        if(!preg_match('/^[0-9]{1,10}$/',$tid))  return ThrowApiExc('20003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,10}$/u',$data['title'])) return ThrowApiExc('20001');
        if(!preg_match('/^[\w \x{4e00}-\x{9fa5}]{2,255}$/u',$data['description'])) return ThrowApiExc('20002');

        $res = DB::table('C_type')->where('id',$tid)->update(
            [
                'title'=>$data['title'],
                'description'=>$data['description']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '修改成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40001');
        }

    }


    /*
     * 删除案件类型的状态以及内容
     *
     * @return json
     * */

    public function delete_case_type($tid){

        if(!preg_match('/^[0-9]{1,10}$/',$tid))  return ThrowApiExc('20003');

        $res=DB::table('C_type')->where('id',$tid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }

    }


}
